/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import pojo.HanhKhachPOJO;

/**
 *
 * @author b2
 */
public class HanhKhachDAO {

    public static ArrayList<HanhKhachPOJO> LayDanhSachHanhKhach() {

        ArrayList<HanhKhachPOJO> ds = new ArrayList<HanhKhachPOJO>();
        try {
            String sql = "SELECT * FROM HANHKHACH ";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                HanhKhachPOJO hk = new HanhKhachPOJO();
                hk.setMaHanhKhach(rs.getString("MaHanhKhach"));
                hk.setTenHanhKhach(rs.getString("TenHanhKhach"));
                hk.setCMND(rs.getString("CMND"));
                hk.setDienThoai(rs.getString("DienThoai"));
                ds.add(hk);
            }
            helper.close();

        } catch (SQLException ex) {
            Logger.getLogger(HanhKhachDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ds;
    }

    public static HanhKhachPOJO LayThongTinHanhKhach(String maHanhKhach) {

       HanhKhachPOJO hk = new HanhKhachPOJO();
        try {
            String sql = "SELECT * FROM HANHKHACH WHERE MaHanhKhach ='" + maHanhKhach + "'";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                
                hk.setMaHanhKhach(rs.getString("MaHanhKhach"));
                hk.setTenHanhKhach(rs.getString("TenHanhKhach"));
                hk.setCMND(rs.getString("CMND"));
                hk.setDienThoai(rs.getString("DienThoai"));
            }
            helper.close();

        } catch (SQLException ex) {
            Logger.getLogger(HanhKhachDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return hk;
    }
    public static ArrayList<HanhKhachPOJO> TimKiemHanhKhachTheoMa(String maHanhKhach) {

        ArrayList<HanhKhachPOJO> ds = new ArrayList<HanhKhachPOJO>();
        try {
            String sql = "SELECT * FROM HANHKHACH WHERE MaHanhKhach LIKE '%" + maHanhKhach + "%'";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                HanhKhachPOJO hk = new HanhKhachPOJO();
                hk.setMaHanhKhach(rs.getString("MaHanhKhach"));
                hk.setTenHanhKhach(rs.getString("TenHanhKhach"));
                hk.setCMND(rs.getString("CMND"));
                hk.setDienThoai(rs.getString("DienThoai"));
                ds.add(hk);
            }
            helper.close();

        } catch (SQLException ex) {
            Logger.getLogger(HanhKhachDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ds;
    }
     public static ArrayList<HanhKhachPOJO> TimKiemHanhKhachTheoTen(String tenHanhKhach) {
        ArrayList<HanhKhachPOJO> ds = new ArrayList<HanhKhachPOJO>();
        try {
            String sql = "SELECT * FROM HANHKHACH WHERE TENHANHKHACH LIKE '%" + tenHanhKhach+ "%'";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                HanhKhachPOJO hk = new HanhKhachPOJO();
                hk.setMaHanhKhach(rs.getString("MaHanhKhach"));
                hk.setTenHanhKhach(rs.getString("TenHanhKhach"));
                hk.setCMND(rs.getString("CMND"));
                hk.setDienThoai(rs.getString("DienThoai"));
                ds.add(hk);
            }
            helper.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return ds;
    }
    //phương thức thêm

    public static boolean themHanhKhach(HanhKhachPOJO hk) {
        boolean kq = false;
        String sql = String.format("INSERT INTO HANHKHACH (`MaHanhKhach`,`TenHanhKhach`,`CMND`,`DienThoai`) VALUES ('%S','%S','%S','%S');", hk.getMaHanhKhach(), hk.getTenHanhKhach(), hk.getCMND(), hk.getDienThoai());
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        int n = helper.executeUpdate(sql);
        if (n == 1) {
            kq = true;
        }
        helper.close();

        return kq;
    }
//phương thức xóa

    public static boolean xoaHanhKhach(String maHanhKhach) {
        boolean kq = false;
        String sql = "DELETE FROM HANHKHACH WHERE MAHANHKACH = '" + maHanhKhach + "'";
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        int i = helper.executeUpdate(sql);
        if (i == 1) {
            kq = true;
        }
        helper.close();

        return kq;
    }
}
